package com.teamin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.teamin.entity.CircleUserEntity;
import com.teamin.entity.MessageEntity;
import com.teamin.entity.UserEntity;
import com.teamin.service.UserService;
import com.teamin.service.WebsocketService;
import com.teamin.web.exception.GlobalErrorInfoException;
import com.teamin.web.socket.SessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

/**
 * 有关websocket 发送消息的服务
 */
@Component
public class WebsocketServiceImpl implements WebsocketService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private SimpMessagingTemplate messagingTemplate;

	@Autowired
	private CircleServiceImpl circleService;

	@Autowired
	private UserService userService ;

	//圈子信息同步
	@Override
	public void sendCircle(CircleUserEntity circleUserEntity,Long circleId,CirclePropertise propertise,Control control,Object value,String userFlag) {

		JSONObject json = new JSONObject();
		json.put("type",10000) ;
		json.put("circleId",circleId) ;
		json.put("propertise",propertise.value() ) ;
		json.put("control",control.value() ) ;
		json.put("userFlag",userFlag) ;
		json.put("userFlag",userFlag) ;
		json.put("value",value ) ;
		json.put("controlUser",getControUserInfo(circleUserEntity)) ;
		//messagingTemplate.convertAndSendToUser("44","/circle/"+circleId,"sdfafsdfs");
		messagingTemplate.convertAndSend("/circle/"+circleId,json);
	}

	//圈子信息同步
	public void sendCircle(Long userId,Long circleId,CirclePropertise propertise,Control control,Object value,String userFlag) {

		JSONObject json = new JSONObject();
		json.put("type",10000) ;
		json.put("circleId",circleId) ;
		json.put("propertise",propertise.value() ) ;
		json.put("control",control.value() ) ;
		json.put("userFlag",userFlag) ;
		json.put("value",value ) ;
		json.put("controlUser",getControUserInfo(userId,circleId)) ;
		//messagingTemplate.convertAndSendToUser("44","/circle/"+circleId,"sdfafsdfs");
		messagingTemplate.convertAndSend("/circle/"+circleId,json);
	}

	/**
	 * 有关websocket 任务发送消息的服务
	 * @param circleId
	 * @param taskId
	 * @param propertise
	 * @param control
	 * @param value
	 */
	@Override
	public void sendTask(Long userId,Long circleId, Long taskId, TaskPropertise propertise, Control control, Object value,String userFlag) {

		JSONObject json = new JSONObject();
		json.put("type",10100) ;
		json.put("circleId",circleId) ;
		json.put("taskId",taskId) ;
		json.put("propertise",propertise.value() ) ;
		json.put("control",control.value() ) ;
		json.put("userFlag",userFlag) ;
		json.put("value",value ) ;
		json.put("controlUser",getControUserInfo(userId,circleId)) ;
		//messagingTemplate.convertAndSendToUser("44","/circle/"+circleId,"sdfafsdfs");
		messagingTemplate.convertAndSend("/task/"+taskId,json);
	}

	/**
	 * 个人消息通知
	 * @param propertise
	 */
	@Override
	public void sendToUserNotifi(Long toUser, Long userId,Long circleId, NotifiPropertise propertise, MessageEntity messageId,String userFlag) {

		if(!SessionManager.userOnline(String.valueOf(toUser)))
			return ;

		JSONObject json = new JSONObject();
		json.put("type",20100) ;
		json.put("propertise",propertise.value() ) ;
		json.put("value",messageId ) ;
		json.put("unreadCount",userService.getUnreadCount(toUser)) ;
		json.put("controlUser",getControUserInfo(userId,circleId)) ;
		json.put("userFlag",userFlag) ;
		json.put("circle",getCircleInfo(circleId)) ;
		//messagingTemplate.convertAndSendToUser("44","/circle/"+circleId,"sdfafsdfs");

		messagingTemplate.convertAndSendToUser(toUser+"","/notification",json);
	}

	/**
	 * 个人消息通知
	 * @param propertise
	 */
	@Override
	public void sendToUserNotifi(Long toUser, Long userId, Long circleId, NotifiPropertise propertise, Object obj,String userFlag) {

		if(!SessionManager.userOnline(String.valueOf(toUser)))
			return ;

		JSONObject json = new JSONObject();
		json.put("type",20100) ;
		json.put("propertise",propertise.value() ) ;
		json.put("value",obj ) ;
		json.put("unreadCount",userService.getUnreadCount(toUser)) ;
		json.put("controlUser",getControUserInfo(userId,circleId)) ;
		json.put("userFlag",userFlag) ;
		json.put("circle",getCircleInfo(circleId)) ;
		//messagingTemplate.convertAndSendToUser("44","/circle/"+circleId,"sdfafsdfs");

		messagingTemplate.convertAndSendToUser(toUser+"","/notification",json);
	}

	/**
	 * 个人消息通知
	 * @param propertise
	 */
	@Override
	public void sendToUserNotifi(Long toUser, CircleUserEntity circleUserEntity, Long circleId, NotifiPropertise propertise, MessageEntity messageId,String userFlag) {

		if(!SessionManager.userOnline(String.valueOf(toUser)))
			return ;

		JSONObject json = new JSONObject();
		json.put("type",20100) ;
		json.put("propertise",propertise.value() ) ;
		json.put("value",messageId ) ;
		json.put("controlUser",getControUserInfo(circleUserEntity)) ;
		json.put("userFlag",userFlag) ;
		json.put("circle",getCircleInfo(circleId)) ;
		//messagingTemplate.convertAndSendToUser("44","/circle/"+circleId,"sdfafsdfs");

		messagingTemplate.convertAndSendToUser(toUser+"","/notification",json);
	}

	/**
	 * 个人消息通知
	 * @param propertise
	 */
	@Override
	public void sendToUserNotifi(Long toUser, CircleUserEntity circleUserEntity, Long circleId, NotifiPropertise propertise, Object obj,String userFlag) {

		if(!SessionManager.userOnline(String.valueOf(toUser)))
			return ;

		JSONObject json = new JSONObject();
		json.put("type",20100) ;
		json.put("propertise",propertise.value() ) ;
		json.put("value",obj ) ;
		json.put("controlUser",getControUserInfo(circleUserEntity)) ;
		json.put("userFlag",userFlag) ;
		json.put("circle",getCircleInfo(circleId)) ;
		//messagingTemplate.convertAndSendToUser("44","/circle/"+circleId,"sdfafsdfs");

		messagingTemplate.convertAndSendToUser(toUser+"","/notification",json);
	}

	/**
	 * 圈子的个人消息通知
	 * @param propertise
	 */
	@Override
	public void sendToCircleUserNotifi(Long toUser, CircleUserEntity circleUserEntity, Long circleId, NotifiPropertise propertise, MessageEntity messageId,String userFlag) {

		if(!SessionManager.userOnline(String.valueOf(toUser)))
			return ;

		JSONObject json = new JSONObject();
		json.put("type",20100) ;
		json.put("propertise",propertise.value() ) ;
		json.put("value",messageId ) ;
		json.put("controlUser",getControUserInfo(circleUserEntity)) ;
		json.put("userFlag",userFlag) ;
		json.put("circle",getCircleInfo(circleId)) ;

		messagingTemplate.convertAndSendToUser(""+toUser,"/circle/"+circleId,json);
	}

	/**
	 * 个人消息通知
	 * @param propertise
	 */
	@Override
	public void sendToCircleUserNotifi(Long toUser, Long userId, Long circleId, NotifiPropertise propertise, Object value,String userFlag) {

		JSONObject json = new JSONObject();
		json.put("type",10200) ;
		json.put("propertise",propertise.value() ) ;
		json.put("controlUser",getControUserInfo(userId,circleId)) ;
		json.put("userFlag",userFlag) ;
		json.put("value",value) ;
		messagingTemplate.convertAndSendToUser(""+toUser,"/circle/"+circleId,json);
	}

	public JSONObject getControUserInfo(Long userId, Long circleId) {

		CircleUserEntity cirUser = circleService.selectMemberFromCircle(circleId, userId);
		JSONObject user = new JSONObject() ;
		if(cirUser == null) {
			UserEntity us = null;
			try {
				us = userService.selectUser(userId);
			} catch (GlobalErrorInfoException e) {
				e.printStackTrace();
			}

			if(us != null ) {
				user.put("userName",us.getUserName()) ;
				user.put("userId",us.getUserId()) ;
				user.put("userHeadIcon",us.getHeadIcon()) ;
			}
		}else {
			user.put("userName",cirUser.getNickName()) ;
			user.put("userId",cirUser.getUserId()) ;
			user.put("userHeadIcon",cirUser.getHeadIcon()) ;
		}



		return user ;
	}

	public JSONObject getControUserInfo(CircleUserEntity cirUser) {
		JSONObject user = new JSONObject() ;
		user.put("userName",cirUser.getNickName()) ;
		user.put("userId",cirUser.getUserId()) ;
		user.put("userHeadIcon",cirUser.getHeadIcon()) ;
		return user ;
	}

	public JSONObject getCircleInfo(Long circleId) {
		String circleName = circleService.selectCircleName(circleId);
		JSONObject circle = new JSONObject() ;
		circle.put("circleId",circleId) ;
		circle.put("circleName",circleName) ;
		return circle ;
	}
}
